package com.user.controller;

import com.api.entity.User;
import com.api.util.RedisUtils;
import com.api.util.ResponseDTO;
import com.user.mapper.UserMapper;
import com.api.util.FileUploadAndDownLoadUtill;
import com.api.util.GetIpAndBrowser;
import com.api.util.JwtUtil;
import com.api.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/*
    @ClassName UserController
    @Description TODO
    @Date 2021/8/31、10:38
*/
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserMapper mapper;

    @Resource
    private JwtUtil jwtUtil;

    @Autowired
    private RedisUtils redisUtils;

    @RequestMapping("/login")
    public ResponseDTO<Object> login(@RequestParam("phone") String phone, @RequestParam("password")String password,
                                     HttpServletRequest request){
        User user = new User(phone,MD5Utils.getMD5String(password));
        User u = mapper.checkUser(user);
        String jwt = "";
        String msg = "登录成功";
        int state = 200;
        Map<String,Object> map = new HashMap<String,Object>();
        if (u!=null){
            //登录成功,生成token
            jwt = jwtUtil.createJWT(u.getId() + "", u.getName(), u);
            map.put("token",jwt);
            map.put("user",u);
            redisUtils.set("user"+u.getId(),jwt,(long)30*60);
        }else{
            //判断手机号是否重复
            if (mapper.checkPhone(user.getPhone()) != null){
                //进来就是密码错误,返回错误信息给前端
                msg = "登录失败，密码错误或用户名不存在";
                state = 300;
            }else{
                //如果不存在相同的手机号，就进行注册
                user.setRegIp(GetIpAndBrowser.getIp(request));
                mapper.addUser(user);
                u = mapper.selectById(user.getId());
                //注册成功后生成token
                jwt = jwtUtil.createJWT(user.getId() + "", user.getName(), user);
                map.put("token",jwt);
                map.put("user",u);
                redisUtils.set("user"+u.getId(),jwt,(long)30*60);
            }
        }

        return new ResponseDTO<>(state, msg, map);
    }

    @RequestMapping("logout")
    public String logout(){

        return "";
    }

    //更新图片、昵称
    @RequestMapping("updateUser")
    public String updateUser(@RequestParam("userId")int id, @RequestParam("newName")String newName, @RequestParam("fileId")String fileId){
        if (fileId == null && "".equals(fileId.trim())){
            fileId = "000";
        }
        mapper.updateUser(id,newName,fileId);
        return "success";
    }

    @RequestMapping("upload")
    public String upload(@PathVariable("form")MultipartFile file){
        //上传文件
        String fileId = FileUploadAndDownLoadUtill.uploadFile(file);
        //上传成功后返回FileName
        return fileId;
    }

    //修改密码
    @RequestMapping("updatePassword")
    public String updatePassword(@RequestParam("userId")int id,@RequestParam("newPwd")String newPwd){
        mapper.updatePwd(id, MD5Utils.getMD5String(newPwd));
        return "success";
    }

    //查看原密码
    @RequestMapping("FindOldPwd")
    public String FindOldPwd(@RequestParam("oldPwd")String oldPwd){

        return MD5Utils.getMD5String(oldPwd);
    }

}
